<?php

namespace App\Services;

use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Iot\Iot;

class LotService {

    private $config ;
    private $PageSize=1;//指定返回结果中每页显示的产品数量
    private $CurrentPage=3;//指定显示返回结果中的第几页
    private $IotInstanceId='iot-06z00jovuqypdck';//实例 ID
    private $ProductKey='h8ttv0EL0EB';//设备所属的产品ProductKey

    public function __construct()
    {
        AlibabaCloud::accessKeyClient(config("dev.accessKeyId"), config("dev.accessKeySecret"))->regionId('cn-shanghai')
            ->asDefaultClient();
    }

    public function DeviceByStatusList(Request $request)
    {
        try {
            $app = self::run();
            $dev = $app->queryDeviceByStatus();
            $list = $dev->withIotInstanceId($this->IotInstanceId)
                ->withStatus($request->status)
                ->withProductKey($this->ProductKey)
                ->withCurrentPage($this->CurrentPage)
                ->withPageSize($this->PageSize)
                ->debug(false)// Enable the debug will output detailed information
                ->connectTimeout(1)// Throw an exception when Connection timeout
                ->timeout(1)// Throw an exception when timeout
                ->request();
            return $list->toArray();
        }catch (ClientException $exception) {
            echo $exception->getMessage() . PHP_EOL;
        } catch (ServerException $exception) {
            echo $exception->getMessage() . PHP_EOL;
            echo $exception->getErrorCode() . PHP_EOL;
            echo $exception->getRequestId() . PHP_EOL;
            echo $exception->getErrorMessage() . PHP_EOL;
        }
    }
    //根据设备状态查询设备列表

    public function run()
    {
        return Iot::v20180120();
    }
    //查询指定产品下的所有设备列表

    public function getQueryDevice(Request $request)
    {
        try {
            $app = self::run();
            $request = $app->queryDevice();
            $result = $request
                ->withIotInstanceId($this->IotInstanceId)
                ->withProductKey($this->ProductKey)
                ->debug(false) // Enable the debug will output detailed information
                ->connectTimeout(3) // Throw an exception when Connection timeout
                ->timeout(3) // Throw an exception when timeout
                ->request();
            return $result->toArray();
        } catch (ClientException $exception) {
            echo $exception->getMessage() . PHP_EOL;
        } catch (ServerException $exception) {
            echo $exception->getMessage() . PHP_EOL;
            echo $exception->getErrorCode() . PHP_EOL;
            echo $exception->getRequestId() . PHP_EOL;
            echo $exception->getErrorMessage() . PHP_EOL;
        }
    }
    //通过自定义Topic向指定设备发布消息
    public function Pub($name, $msgarray)
    {
//        if($status==1){
//            $msg=base64_encode('{"SW_LED":"ON"}');
//        }else{
//            $msg=base64_encode('{"SW_LED":"OFF"}');
//        }
        $msg = base64_encode(json_encode($msgarray));
        $topname="/h8ttv0EL0EB/{$name}/user/TEMP_TEST";
        try {
            $app = self::run();
            $request = $app->pub();
            $result = $request
                ->withIotInstanceId($this->IotInstanceId)
                ->withProductKey($this->ProductKey)
                ->withQos(1)
                ->withTopicFullName($topname)
                ->withMessageContent($msg)
                ->debug(false) // Enable the debug will output detailed information
                ->connectTimeout(3) // Throw an exception when Connection timeout
                ->timeout(3) // Throw an exception when timeout
                ->request();
            return $result->toArray();
        } catch (ClientException $exception) {
            echo $exception->getMessage() . PHP_EOL;
        } catch (ServerException $exception) {
            echo $exception->getMessage() . PHP_EOL;
            echo $exception->getErrorCode() . PHP_EOL;
            echo $exception->getRequestId() . PHP_EOL;
            echo $exception->getErrorMessage() . PHP_EOL;
        }
    }



}
